package ArrayTest.Homework.ArmyTest;

public class Army {
    private Weapon[] weapons;
    int index = 0;
    public Army(int i){
        this.weapons = new Weapon[i];
    }

    public Weapon[] getWeapons() {
        return weapons;
    }

    public void setWeapons(Weapon[] weapons) {
        this.weapons = weapons;
    }

    public void addWeapon(Weapon wa){  //武器入库
        if(index< weapons.length){
            this.weapons[index++] = wa;
            System.out.println(wa+"添加成功");
        }else{
            System.out.println("添加武器失败，该武器库已满");
        }
    }

    public void moveAll(){     //遍历数组，如果为Move类型则开启支援
        for(int i = 0; i < index; i++){
            if(weapons[i] instanceof Move){
                Move m = (Move)weapons[i];
                m.move();
            }
        }
    }
    public void attackAll(){  //遍历数组，如果为Attack类型则发起攻击
        for(int i = 0; i < index; i++){
            if(weapons[i] instanceof Attack){
                Attack a = (Attack)weapons[i];
                a.attack();
            }
        }
    }
}
